package me.zhengjie.modules.system.domain

import cn.hutool.core.bean.BeanUtil
import cn.hutool.core.bean.copier.CopyOptions
import com.fasterxml.jackson.annotation.JsonTypeName
import me.zhengjie.common.utils.JacksonUtil
import me.zhengjie.modules.system.service.dto.DictDTO
import jakarta.persistence.*
import org.apache.commons.lang3.builder.ToStringBuilder
import org.springframework.data.redis.core.RedisHash


/**
 * @author kuki
 * @date 2022-07-21
 */
@Entity
@Table(name = "sys_province")
@JsonTypeName("Province")
@RedisHash("province")
data class Province (
    // 序号
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    val id: Int? = null,

    // 省份名称
    @Column(name = "name", nullable = false)
    val name: String? = null,

    // 省份名称
    @Column(name = "name_chs", nullable = false)
    val nameChs: String? = null,

    // 区省份名称
    @Column(name = "name_en", nullable = false)
    val nameEn: String? = null,

    // 省份代码
    @Column(name = "province_code", nullable = false)
    val provinceCode: Long? = null
){
    annotation class Update

    companion object {
        fun fromJson(json: String): Province? {
            val obj = JacksonUtil.parseObject(json, Province::class.java)!!
            return when (obj) {
                is Province -> obj
                else -> null
            }
        }
    }

    fun tojson(): String? {
        return JacksonUtil.toJSONString(this)
    }

    fun copy(source: Province?) {
        BeanUtil.copyProperties(source, this, CopyOptions.create().setIgnoreNullValue(true))
    }
}